<?php
class TinhsController extends AppController {
	//var $components = array ('Report');
	var $name = 'Tinhs';
	var $helpers = array('Html', 'Form');
	var $paginate = array('limit' => 15, 'page' => 1, 'order' =>array('Tinh.Ten_T'=>'asc'));
	
	
	function beforeFilter(){
        parent::beforeFilter();
        $curUser = $this->Auth->user();
        if($curUser['User']['ROLE']!='admin'&&$curUser['User']['ROLE']!='user'){
            $this->Session->setFlash('Bạn phải đăng nhập quyền ADMIN để thực hiện chức năng này');
            $this->redirect('/pages');
        }
    }
    
	function index() {
		$this->Tinh->recursive = 0;
		$this->set('tinhs', $this->paginate());
		$params = $this->params['pass'];
	/*	  if($session['User']['ROLE']=='admin')
  			 $this->set('tinhs', $this->paginate('Tinh'));
 		 else
   			$this->set('lua_msg','Bạn phải đăng nhập bằng quyền admin');*/
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Tinh.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('tinh', $this->Tinh->read(null, $id));
	}
function viewPdf($id = null)
    {
        if (!$id)
        {
            $this->Session->setFlash('Sorry, there was no property ID submitted.');
            $this->redirect(array('action'=>'index'), null, true);
        }
        Configure::write('debug',0); // Otherwise we cannot use this method while developing

        $id = intval($id);

        $property = $this->__view($id); // here the data is pulled from the database and set for the view

        if (empty($property))
        {
            $this->Session->setFlash('Sorry, there is no property with the submitted ID.');
            $this->redirect(array('action'=>'index'), null, true);
        }

        $this->layout = 'pdf'; //this will use the pdf.ctp layout
        $this->render();
    } 
	function add() {
		if (!empty($this->data)) {
			$this->Tinh->create();
			if ($this->Tinh->save($this->data)) {
				$this->Session->setFlash(__('Tỉnh đã lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Không thể lưu. Yêu cầu xem lại dữ liệu vừa nhập.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Tinh', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Tinh->save($this->data)) {
				$this->Session->setFlash(__('Tỉnh đã lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Không thể lưu. Yêu cầu xem lại dữ liệu vừa nhập.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tinh->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Tinh', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Tinh->del($id)) {
			$this->Session->setFlash(__('Tinh deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
function createreport()
	{
		if (!empty($this->data)) 
		{ 
			//Determine if user is pulling existing report or deleting report
			if(isset($this->params['form']['existing']))
			{
				if($this->params['form']['existing']=='Pull')
				{
					//Pull report
					$this->Report->pull_report($this->data['Misc']['saved_reports']);
				}
				else if($this->params['form']['existing']=='Delete')
				{
					//Delete report
					$this->Report->delete_report($this->data['Misc']['saved_reports']);

					//Return user to form
					$this->flash('Your report has been deleted.','/'.$this->name.'/'.$this->action.'');
				}
			}
			else
			{
				//Filter out fields
				$this->Report->init_display($this->data);
				
				//Set sort parameter
				if(!isset($this->params['form']['order_by_primary'])) { $this->params['form']['order_by_primary']=NULL; }
				if(!isset($this->params['form']['order_by_secondary'])) { $this->params['form']['order_by_secondary']=NULL; }
				$this->Report->get_order_by($this->params['form']['order_by_primary'], $this->params['form']['order_by_secondary']);

				//Store report name
				if(!empty($this->params['form']['report_name']))
				{
					$this->Report->save_report_name($this->params['form']['report_name']);
				}

				//Store report if save was executed
				if($this->params['form']['submit']=='Create And Save Report')
				{
					if(empty($this->params['form']['report_name']))
					{
						//Return user to form
						$this->flash('Must enter a report name when saving.','/'.$this->name.'/'.$this->action.'');
					}
					else
					{
						$this->Report->save_report();
					}
				}
			}
			
			//Set report fields
			$this->set('report_fields', $this->Report->report_fields);

			//Set report name
			$this->set('report_name', $this->Report->report_name);

			//Allow search to go 2 associations deep
			$this->{$this->modelClass}->recursive = 2;

			//Set report data
			$this->set('report_data', $this->{$this->modelClass}->findAll(NULL,NULL,$this->Report->order_by));
		} 
		else
		{
			//Setup options for report component
			/*
				You can setup a level two association by doing the following:
				"VehicleDriver"=>"Employee" ie $models = Array ("Vehicle", "VehicleDriver"=>"Employee");
				Please note that all fields within a level two association cannot be sorted.
			*/
			$models =	Array ("");

			//Set array of fields
			$this->set('report_form', $this->Report->init_form($models));

			//Set current controller
			$this->set('cur_controller', $this->name);

			//Pull all existing reports
			$this->set('existing_reports', $this->Report->existing_reports());
		}
	}	



}
?>